package com.cloud.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.util.AttributeSet;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatTextView;

/**
 * @Author： Chen
 * @Date： 2020/6/30
 * @Desc：
 */
public class BarCodeText extends AppCompatTextView {
    public BarCodeText(Context context) {
        super(context);
    }

    public BarCodeText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BarCodeText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void setText(CharSequence text, TextView.BufferType type) {
        if (TextUtils.isEmpty(text)) {
            super.setText(text, type);
            return;
        }
        int start = 0;
        if (text.length() > 5) {
            start = text.length() - 5;
        }
        SpannableString spanString = new SpannableString(text);
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#218dfc"));
        spanString.setSpan(colorSpan, start, text.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        int textSize = (int) getTextSize() + 2;
        AbsoluteSizeSpan sizeSpan = new AbsoluteSizeSpan(textSize);
        spanString.setSpan(sizeSpan, start, text.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
        spanString.setSpan(boldSpan, start, text.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        super.setText(spanString, type);
    }
}
